import { Component, OnInit, Injector } from '@angular/core';
import { NgZorroPageBaseComponent } from '../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import { BaseComponentConfig } from '../../core/model/BaseComponentConfig';
import { ArSystemFormControl, SystemFormControl, FieldType, LoadRemoteDataType } from '../..';
import { map } from '../../../../node_modules/rxjs/operators';
import { ExpressionPair, Compare, AndOr } from '../../core/model/CommonDto';
import { NavigationExtras } from '../../../../node_modules/@angular/router';

@Component({
  selector: 'app-role-template',
  templateUrl: './role-template.component.html',
  styleUrls: ['./role-template.component.scss']
})
export class RoleTemplateComponent extends NgZorroPageBaseComponent implements OnInit {

  subjectId: number;

  constructor(
    inject: Injector
  ) {
    super(inject,
      new BaseComponentConfig(
        "DrillRecord/RolePlayTemplateRecords",
        "",
        "",
        "",
        "DrillRecord/DeleteDrillRecord",
        "id",
        "",
        "演练主题管理",
        null,
        '',
        { Edit: true, Dell: true }
      ), new ArSystemFormControl([
        new SystemFormControl("subjectName", "主题名称", FieldType.Text, true, 30, 0, "", "", false, null, LoadRemoteDataType.LoadAll, true, true, false),
        new SystemFormControl("drillType", "演练类型", FieldType.Text, false, 0, 0, "", "", false, null, LoadRemoteDataType.LoadAll, true, true, false),
        new SystemFormControl("creationTime", "创建时间", FieldType.Datetime, false, 30, 0, "", "", false, null, LoadRemoteDataType.LoadAll, true, false, false),
        new SystemFormControl("drillStatus", "演练状态", FieldType.Text, false, 0, 0, "", "", false, null, LoadRemoteDataType.LoadAll, true, true, false),
      ]));
  }

  ngOnInit() {
    this.route.queryParamMap
      .pipe(map(params => params.get('id') || null))
      .subscribe(e => {
        this.subjectId = Number(e);
        if (!this.subjectId) {
          this.message.error('无法确定演练内容！');
          throw new Error('无法确定演练内容！');
        }
        this.IniObserver.subscribe(sb => {
          this.listSearchValue = new Array<ExpressionPair>(
            new ExpressionPair('subjectId', this.subjectId, Compare.Equal, AndOr.And)
          );
          this.getEntityList();
        });
      });
  }
  getNewEntity(): any {
    return {};
  }

  /**
   * 创建演练
   */
  gotoDrill() {
    this.nzModalService.confirm({
      nzTitle: '创建',
      nzContent: '是否要创建演练模板内容？',
      nzOnOk: async () => {
        const result = await this.httpService.get<any>(`DrillRecord/SaveRoleRecord?subjectId=${this.subjectId}`);
        const navigationExtras: NavigationExtras = {
          queryParams: { 'id': result }
        };
        this.router.navigate(['/mydrillcontent'], navigationExtras);
      },
      nzOnCancel: () => {

      }
    });
  }

  // 开始演练
  async startDrill(id) {
    const startpar = { Id: id };
    const result = await this.httpService.post<any>('DrillRecord/StartDrillRecord', startpar);
    const navigationExtras: NavigationExtras = {
      queryParams: { 'id': id }
    };
    this.router.navigate(['/mydrillcontent'], navigationExtras);
  }
  /**
 * 删除单个内容
 * @param id 主键
 */
  async deleteEntity(id: number) {
    const msgId = this.message.loading('正在执行删除中', { nzDuration: 0 }).messageId;
    try {
      let body: any;
      if (this.componentConfig.PKeyFieldName && this._pKeyFieldValue) {
        body = {
          "id": this._pKeyFieldValue,
          "ids": [id]
        };
      } else {
        body = [id];
      }
      await this.httpService.get<any>(this.componentConfig.DeleteUrl + '?input=' + id);
      await this.getEntityList();
    } finally {
      this.message.remove(msgId);
    }

  }
}
